package com.tensquare.sms.util;

import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/28 20:30
 */
@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    private String template_code; //模板编号
    @Value("${aliyun.sms.sign_name}")
    private String singn_name;//签名

    /**
     * fetch data by rule id
     *
     * @description  发送短信
     * @param
     * @return
     */
    @RabbitHandler
    public void sendSms(Map<String,String> message){
        System.out.println("手机号："+message.get("mobile"));
        System.out.println("验证码："+message.get("code"));

        try {
            smsUtil.sendSms(message.get("message"),template_code,singn_name,"{\"number\":\""+message.get("code")+"\"}");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
